from pylab import *
import random
import base

"""
This file contains functions/script that creates n number of airports, 
and m number of flights operating in between them. There are many ways 
to do it randomly or it can be taken from a real data. This file contains
following methods:

Random Generation
-------------------
Input:   ndt<type:int>    Number of time periods
         n                Number of airports
         m                Number of flights

Airport: A list of n number of airports are created, capacity of all the 
         airports are kept constant (default value set in base.py)

Flights: First a list of m number of flights are created, then they are 
         assigned a departure and arrival time randomly from the set of 
         time periods. It is taken care that last time period is not 
         assigned as departure time of any flight, also departure time
         is less then the arrival time.
      
         After that airports are selected at random and assigned to flights,
         taking into consideration that arrival and departure airport should
         not be the same.

         Then some flights are selected at random and assigned next flight 
         and at the same time the corresponding flight is linked to prev 
         flight.

Output<type:list>: [Airports<type:list>, Flights<type:list>]

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From data
-------------------

"""

def read_airport_name(n):
    """
    Reads name of the airport from a text file. It is redundant as far as 
    out model in concerened.
    """ 
    file = open("airportname.txt")
    tmp = []
    while 1:
        lines = file.readlines(100000)
        if not lines:
            break
        for line in lines:
            tmp.append(line)
        airport = []
        airport.extend([tmp.pop(random.randint(0,len(tmp))) for i in range(n)])
        return airport





def RandomGeneration(ndt, n, m, D, R):
    Airports = []
    Flights = []
    Time = [i for i in range(ndt)]
    str_air = read_airport_name(n)
    #Creating airport and assigning then names
    for i in range(n):
            str_1 = str_air[i].partition(":")
            str_2 = str_1[2].partition(":")[2].partition(":")[0]
            Airports.append(base.airport(i, str_1[0], str_2, ndt, D=D, R=R))
    
    #Creating Flights
    Flights.extend([base.flight(i, "AI 11"+ str(i)) for i in range(m)])

    # Setting up flight info
    for i in Flights:
        #setting time
        
        dtime = random.choice(Time[:ndt])
        atime = random.choice(Time[:ndt])
        
        while atime < dtime:
            atime = random.choice(Time[:ndt])
        
        #setting airports
        dairport = random.choice(Airports)
        aairport = random.choice(Airports)
        
        while dairport == aairport:
            aairport = random.choice(Airports)
        
        i.setup(dairport,dtime,aairport,atime,10,80)
        
    #setting up next flight
    for i in Flights:
        tmp = []
    
        for j in Flights:
            if j.k_d == i.k_a:
                tmp.append(j)
                tmp.append(None)
        
        if tmp != []:
            tmpn = []
            for j in tmp:
                if  j != None:
                    if j.d > i.r:
                        tmpn.append(j)
            if tmpn != []:
                randtmp = random.choice(tmpn)
            
                if randtmp != None:
                    i.next_flight = randtmp
                    randtmp.prev_flight = i
    return [Airports, Flights]
        
def _print(Flights):
    for i in Flights:
        print "Flight ", i.code 
        print "Departure Airport", i.k_d.code, "Time ", i.d
        print "Arrival Airport", i.k_a.code, "Time ", i.r
        if i.next_flight != None:
            print "Next Flight ", i.next_flight.code
        if i.prev_flight != None:
            print "Prev Flight", i.prev_flight.code
        print "##############################################"
            
    
def MyTestGenerator1(ndt):
    Airports = []
    Airports.append(base.airport(0, 'BHO', 'Raja Bhoj International Airport, Bhopal', ndt, D=2, R=1))
    Airports.append(base.airport(1, 'MUM', 'Chatrapati Shivaji International Airport, Mumbai', ndt, D=2, R=1))

    Flights = []
    Flights.append(base.flight(0,'IC 133'))
    Flights.append(base.flight(1,'KG 155'))
    Flights.append(base.flight(2,'IG 120'))
    Flights.append(base.flight(3,'IC 189'))

    Flights[0].setup(Airports[0],0,Airports[1],1,10,80)
    Flights[1].setup(Airports[0],0,Airports[1],1,10,80)
    Flights[2].setup(Airports[1],0,Airports[0],1,10,80)
    Flights[3].setup(Airports[1],0,Airports[0],1,10,80)

    return [Airports, Flights]

def MyTestGenerator2(ndt):
    Airports = []
    Airports.append(base.airport(0, 'BHO', 'Raja Bhoj International Airport, Bhopal', ndt, D=2, R=1))
    Airports.append(base.airport(1, 'MUM', 'Chatrapati Shivaji International Airport, Mumbai', ndt, D=2, R=1))

    Flights = []
    Flights.append(base.flight(0,'IC 133'))
    Flights.append(base.flight(1,'KG 155'))
    Flights.append(base.flight(2,'IG 120'))
    Flights.append(base.flight(3,'IC 189'))
    Flights.append(base.flight(4,'IC 201'))
    Flights.append(base.flight(5,'IG 999'))

    Flights[0].setup(Airports[0],0,Airports[1],1,10,80)
    Flights[1].setup(Airports[0],0,Airports[1],1,10,80)
    Flights[2].setup(Airports[1],0,Airports[0],1,10,80)
    Flights[3].setup(Airports[1],0,Airports[0],1,10,80)
    Flights[4].setup(Airports[1],2,Airports[0],3,10,80)
    Flights[5].setup(Airports[1],2,Airports[0],3,10,80)
   
    Flights[4].prev_flight = Flights[0]
    Flights[0].next_flight = Flights[4]

    Flights[5].prev_flight = Flights[1]
    Flights[1].next_flight = Flights[5]

    return [Airports, Flights]

def PaperTestGenerator(ndt):
    
    Airports = []
    Airports.append(base.airport(0, 'BHO', 'Raja Bhoj International Airport, Bhopal', ndt, D=100, R=10))
    Airports.append(base.airport(1, 'BOM', 'Chatrapati Shivaji International Airport, Mumbai', ndt, D=100, R=10))
    Airports.append(base.airport(2, 'DEL', 'Indira Gandhi International Airport, New Delhi', ndt, D=2, R=10))

    Time = [i for i in range(ndt)]

    Flights = []
    #Creating Flights
    Flights.extend([base.flight(i, "AI 11"+ str(i)) for i in range(1800)])

    # Setting up flight info
    for i in Flights:
        #setting time
        
        dtime = random.choice(Time[:ndt])
        atime = random.choice(Time[:ndt])
        
        while atime < dtime:
            atime = random.choice(Time[:ndt])
        
        #setting airports
        if Flights.index(i) < 600:
            dairport = Airports[0]
            aairport = random.choice([Airports[1],Airports[2]])
        elif Flights.index(i) >= 600 and Flights.index(i) < 1200:
            dairport = Airports[1]
            aairport = random.choice([Airports[0],Airports[2]])
        else:
            dairport = Airports[2]
            aairport = random.choice([Airports[0],Airports[1]])
        
        
        
        
        i.setup(dairport,dtime,aairport,atime,50,75)
        
    #setting up next flight
    Flight_c = copy(Flights)
    Flight_copy = []
    for i in Flight_c:
        Flight_copy.append(i)
    
    
    tmp_f = []
    for i in range(600):
        tmpindex = random.randint(0,len(Flight_copy)-1)
        tmp_f.append(Flight_copy.pop(tmpindex))

    
    
    for i in Flight_copy:            
        if tmp_f != []:
            tmpn = []
            for j in tmp_f:
                if  j != None:
                    if j.d > i.r:
                        tmpn.append(j)
            if tmpn != []:
                randtmp = random.choice(tmpn)
            
                if randtmp != None:
                    i.next_flight = randtmp
                    randtmp.prev_flight = i
    return [Airports, Flights] 


if __name__ == "__main__":
    tmp = RandomGeneration(10,10,100)
    _print(tmp[1])
